home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / Sample Compress Picture / SampleCompressPicture.c < prev    next >
C/C++ Source or Header  |  1991-09-05  |  8KB  |  394 lines

  1.  
  2.  
  3. #include    <Types.h>
  4. #include    <Files.h>
  5. #include    <Quickdraw.h>
  6. #include    <Packages.h>
  7. #include    <Memory.h>
  8. #include    <Fonts.h>
  9. #include    <Events.h>
  10. #include    <OSUtils.h>
  11. #include    <ToolUtils.h>
  12. #include    <Menus.h>
  13. #include    <Dialogs.h>
  14. #include    <stdio.h>
  15. #include    <Errors.h>
  16. #include    <string.h>
  17.  
  18. #ifndef    THINK_C
  19. #include    <Strings.h>
  20. #endif
  21.  
  22.  
  23. #include    <Image Compression.h>
  24. #include    <CompressionDialog.h>
  25.  
  26.  
  27.  
  28. /************************************************
  29.  *
  30.  *    Set up application environment.
  31.  *
  32.  ************************************************/
  33.  
  34.  
  35.  
  36. Initialize()
  37.  
  38. {
  39.     MaxApplZone();
  40.     InitGraf(&qd.thePort);
  41.     InitFonts();
  42.     InitWindows();
  43.     InitMenus();
  44.     InitDialogs(nil);
  45.     InitCursor();
  46.     
  47. }
  48.  
  49. /************************************************
  50.  *
  51.  *    Make a new name for the compressed file ( illustrative only, this is not the recommended method ).
  52.  *
  53.  ************************************************/
  54.  
  55. GetNewName(char *newName,char *oldName) 
  56.  
  57. {
  58.     
  59.     int    cnum = 1;
  60.     char bname[32];
  61.     int    olen = oldName[0];
  62.     int    c;
  63.     
  64.     strcpy(newName+1,"Compy of ");
  65.     newName[0] = strlen(newName+1);
  66.     if ( olen > 31 )
  67.         olen = 31;
  68.     BlockMove(oldName+1,bname,olen);
  69.     if  ( ( (c=strncmp(oldName+1,newName+1,newName[0])) == 0 ) ||  (sscanf(oldName+1,"Compy %d of %31c",&cnum,bname) == 2) ) {
  70.         if ( c == 0  ) {
  71.             olen -= newName[0];
  72.             BlockMove(bname+newName[0],bname,olen);
  73.         }
  74.         cnum++;
  75.         sprintf(newName+1,"Compy %d of ",cnum);
  76.         newName[0] = strlen(&newName[1]+1);
  77.     }
  78.     if ( olen > (31-newName[0]) )
  79.         olen = 31-newName[0];
  80.     BlockMove(bname,newName+1+newName[0],olen);
  81.     newName[0] += olen;
  82. }
  83.  
  84.  
  85.  
  86. pascal OSErr
  87. Progress(short progressMsg,Fixed progressPercent,long refcon)
  88.  
  89. {
  90.  
  91.     OSErr  result = noErr;
  92.     short kind;
  93.     Handle h;
  94.     Rect r;
  95.     GrafPtr savePort;
  96.     static DialogPtr progressDialog = 0;
  97.     
  98.  
  99.     switch (progressMsg) {
  100.     case codecProgressOpen:
  101.         
  102.         progressDialog = 0;
  103.         if ( (progressDialog = GetNewDialog(5002, 0, (WindowPtr)-1)) != nil ) { 
  104.             ShowWindow((WindowPtr)progressDialog);
  105.             SetPort((GrafPtr)progressDialog);
  106.             DrawDialog(progressDialog);
  107.             GetDItem(progressDialog, 1, &kind, &h, &r);
  108.             InsetRect(&r, -1, -1);
  109.             FrameRect(&r);
  110.             InsetRect(&r, 1, 1);
  111.         }
  112.         break;
  113.  
  114.     case codecProgressUpdatePercent:
  115.     
  116.         GetPort(&savePort);
  117.         if ( progressDialog ) {
  118.             SetPort((GrafPtr)progressDialog);
  119.             GetDItem(progressDialog, 1, &kind, &h, &r);
  120.             r.right = r.left + FixRound(FixMul(progressPercent, FixRatio(r.right-r.left, 1)));
  121.             FillRect(&r, qd.gray);
  122.         }    
  123.         
  124.         /***** it would be cool to look for Cmd Period here ******/
  125.  
  126.         SetPort(savePort);
  127.         break;
  128.     
  129.     case codecProgressClose:
  130.     
  131.         if ( progressDialog )
  132.             DisposDialog(progressDialog);
  133.         progressDialog = 0;
  134.         break;
  135.     }
  136.     return(result);
  137. }
  138.     
  139. CompressPictures()
  140.  
  141. {
  142.     
  143.     /* for original picture */
  144.  
  145.     short        originalFile;
  146.     PicHandle    originalPicture = nil;
  147.     Rect        pictureFrame;
  148.     OpenCPicParams    header;
  149.  
  150.     /* for compressed picture */
  151.     
  152.     short        compressedFile;
  153.     PicHandle    compressedPicture = nil;
  154.  
  155.  
  156.     /* for sepecifying compression */
  157.     
  158.     short        cDepth = 0;
  159.     CodecType    cMethod = 0;
  160.     CodecQ        cQuality = codecNormalQuality;
  161.  
  162.  
  163.     /* user interface stuff */
  164.     
  165.     char        newName[32];
  166.     CWindowPtr    window;
  167.     Rect        windRect;
  168.     Cursor        **watch;
  169.  
  170.     /* StdFile stuff */
  171.     
  172.     Point        pt = {100,100};
  173.     SFReply        sfr;    
  174.     SFTypeList     types = { 'PICT',0 };
  175.  
  176.     /* generic variables */
  177.     
  178.     OSErr        result;
  179.     ProgressProcRecord    *progP,progressRec;
  180.     
  181.     
  182.     progressRec.progressProc = Progress;
  183.     progressRec.progressRefCon = 0;
  184.     progP = &progressRec;
  185.     
  186.  
  187.     watch =  GetCursor(watchCursor);
  188.  
  189.  
  190.  
  191.     /************************************************
  192.      *
  193.      *    Ask for a pict file to compress
  194.      *
  195.      ************************************************/
  196.         
  197.         SFGetFile(pt,(ConstStr255Param)"",nil,1,types,nil,&sfr);
  198.         if ( !sfr.good  ) {
  199.             result = -1;
  200.             goto done;
  201.         }
  202.  
  203.     /************************************************
  204.      *
  205.      *    Open the file.
  206.      *
  207.      ************************************************/
  208.     
  209.         SetCursor(*watch);
  210.         if ((result=FSOpen(sfr.fName,sfr.vRefNum,&originalFile)) != noErr ) {
  211.             goto done;
  212.         }
  213.  
  214.     
  215.     /************************************************
  216.      *
  217.      *    Get the picture frame, to see how big of a window to make.
  218.      *
  219.      ************************************************/
  220.  
  221.         if ( (result=GetPictureFileHeader(originalFile,&pictureFrame,&header)) != noErr ) {
  222.             goto done;
  223.         }
  224.     
  225.     
  226.     /************************************************
  227.      *
  228.      *    Create a window the size of the picture, and set our port to it.
  229.      *
  230.      ************************************************/
  231.     
  232.         windRect = pictureFrame;
  233.         OffsetRect(&windRect,40,40);
  234.         if ( (window = (CWindowPtr)NewCWindow(nil,&windRect,sfr.fName,true,0,
  235.             (WindowPtr)-1,false,0)) == nil ) {
  236.             result = -1;
  237.             goto done;
  238.         }
  239.         SetPort((GrafPtr)window);
  240.         
  241.     /************************************************
  242.      *
  243.      *        Draw the picture in the window, slid up to the top left corner.
  244.      *
  245.      ************************************************/
  246.     
  247.         OffsetRect(&pictureFrame,-pictureFrame.left,-pictureFrame.top);
  248.         if ( (result=DrawPictureFile(originalFile,&pictureFrame,progP)) != noErr ) {
  249.             goto done;
  250.         }
  251.  
  252.  
  253.     /************************************************
  254.      *
  255.      *        Ask how the user wants to compress it.
  256.      *
  257.      ************************************************/
  258.     
  259.         SetCursor(&qd.arrow);
  260.         if ( (result=CompressionDialog(window->portPixMap,&window->portRect,&cQuality,
  261.             &cMethod,nil,&cDepth,nil)) != noErr ) {
  262.             goto done;
  263.         }
  264.  
  265.  
  266.     /************************************************
  267.      *
  268.      *        Ask her for the name of the new file.
  269.      *
  270.      ************************************************/
  271.  
  272.     
  273.         GetNewName(newName,(char *)sfr.fName);
  274.         SFPutFile(pt,(ConstStr255Param)"",(ConstStr255Param)newName,NULL,&sfr);
  275.         if ( !sfr.good )  {
  276.             result = -1;
  277.             goto done;
  278.         }
  279.         BlockMove(sfr.fName,newName,32);
  280.         SetCursor(*watch);
  281.     
  282.     /************************************************
  283.      *
  284.      *    Create the new file, if we can.
  285.      *
  286.      ************************************************/
  287.     
  288.         FSDelete(sfr.fName,sfr.vRefNum);
  289.         if ( (result=Create(sfr.fName,sfr.vRefNum,'ppxi','PICT')) != noErr ) {
  290.             FSClose(originalFile);
  291.             goto done;
  292.         }
  293.         if ( (result=FSOpen(sfr.fName,sfr.vRefNum,&compressedFile)) != noErr ) {
  294.             FSClose(originalFile);
  295.             goto done;
  296.         }
  297.  
  298.     /************************************************
  299.      *
  300.      *    Now the hard part: Compress the PICT and put it into the new file.
  301.      *
  302.      ************************************************/
  303.     
  304.     
  305.         if ( (result=FCompressPictureFile(originalFile,compressedFile,cDepth,nil,cQuality,
  306.             false,false,progP,cMethod,anyCodec)) != noErr ) {
  307.             FSClose(originalFile);
  308.             FSDelete(sfr.fName,sfr.vRefNum);
  309.             FlushVol(nil,sfr.vRefNum);
  310.             goto done;
  311.         }
  312.  
  313. whew:
  314.  
  315.     /************************************************
  316.      *
  317.      *    Change the window name, and draw the compressed PICT in it.
  318.      *
  319.      ************************************************/
  320.  
  321.         EraseRect(&pictureFrame);
  322.         SetWTitle((WindowPtr)window,(ConstStr255Param)newName);
  323.         if ( compressedPicture )
  324.             DrawPicture(compressedPicture,&pictureFrame);
  325.         else
  326.             DrawPictureFile(compressedFile,&pictureFrame,progP);
  327.         SetCursor(&qd.arrow);
  328.     
  329.     
  330.     /************************************************
  331.      *
  332.      *    Close the files and wait for click to exit.
  333.      *
  334.      ************************************************/
  335.     
  336.         FSClose(compressedFile);
  337.         FSClose(originalFile);
  338.         if ( originalPicture )
  339.             KillPicture(originalPicture);
  340.         if ( compressedPicture )
  341.             KillPicture(compressedPicture);
  342.         FlushVol(nil,sfr.vRefNum);
  343.         while (!Button() )
  344.             ;
  345.         CloseWindow((WindowPtr)window);
  346. done:
  347.     return(result);
  348. }
  349.  
  350.  
  351.  
  352.  
  353.  
  354. /************************************************
  355.  *
  356.  *    Our program.
  357.  *
  358.  ************************************************/
  359.  
  360.  
  361. main()
  362.  
  363.  
  364. {
  365.  
  366.     /************************************************
  367.      *
  368.      *    Initialize everything, and then do it. Notice
  369.      *    the wonderful HIG conformant user interface.
  370.      *
  371.      ************************************************/
  372.  
  373.     
  374.     Initialize();
  375.     
  376. #ifdef    LINKED_CODEC_DEBUG    
  377.  
  378.     InstallDebugCodecMgr();
  379.  
  380. #endif
  381.  
  382.     while ( 1 ) {
  383.     
  384.     
  385.         if ( CompressPictures() )
  386.             break;
  387.     }
  388.     
  389. #ifdef    LINKED_CODEC_DEBUG    
  390.     
  391.     DeinstallDebugCodecMgr();
  392. #endif
  393. }
  394.